﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;

using System.Management;
using io = System.IO;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.NetworkInformation;

namespace MOJsdk
{
    public static class Extensions
    {
        public static byte[] SubArray(this byte[] bytes, int StartIndex, int Offset)
        {
            byte[] store = new byte[Offset];
            for (int i = 0; i < Offset; i++)
                store[i] = bytes[i + StartIndex];

            return store;
        }
        public static bool IsInt(this string str)
        {
            try
            {
                str.ToInt();
                return true;
            }
            catch { return false; }
        }
        public static bool IsNullorEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }//Check if string is Null or Empty
        //Convert a list of strings to a unique string using a string [div] as diviser
        public static string ToString(this IEnumerable<string> list, string div = ";")
        {
            if (list.Count() == 0) return "";

            string p = "";
            foreach (string s in list)
                p += s + div;

            if (p[p.Length - 1].ToString() == div)
                p = p.Substring(0, p.Length - 1);

            return p;
        }
        //Convert a string to a list of strings using a string [div] as diviser
        public static IList<string> ToList(this string str, string div=";")
        {
            List<string> list = new List<string>();

            if (str.IsNullorEmpty())
                return list;

            int x = -1;

            while (true)
            {
                x = str.IndexOf(div);
                if (x == -1)
                {
                    list.Add(str);
                    break;
                }

                list.Add(str.Substring(0, x));
                x += div.Length;
                if (x >= str.Length) break;

                str = str.Substring(x);
            }

            return list;
        }

        //Remove invalid path chars
        public static string GetValidPathName(this string txt)
        {
            foreach (var x in io.Path.GetInvalidFileNameChars())
            {
                while (true)
                {
                    int z = txt.IndexOf(x);

                    if (z == -1) break;

                    txt = txt.Remove(z);
                }
            }

            return txt;
        }

        //True if double is NaN
        public static bool IsNaN(this double val)
        {
            return double.IsNaN(val);
        }

        public static int ToInt(this string str)
        {
            try
            {
                return Convert.ToInt32(str);
            }
            catch
            {
                throw new InvalidCastException("*Cant Convert to INT*");
            }
        }
        public static bool ToBool(this string str)
        {
            try
            {
                return Convert.ToBoolean(str);
            }
            catch
            {
                throw new InvalidCastException("*Cant Convert to BOOL*");
            }
        }
        public static double ToDouble(this string str)
        {
            try
            {
                return Convert.ToDouble(str);
            }
            catch
            {
                throw new InvalidCastException("*Cant Convert to Double*");
            }
        }

        public static void ApplyStyle(this FrameworkElement elem, string TheID)
        {
            ElementInfo _info = new ElementInfo()
            {
                ElementType = elem.GetType(),
                ElementID = TheID,
                Element = elem,
            };

            elem.Style = MojHost.GetHost.LoadStyle(_info);
        }
        public static void ApplyStyle(this FrameworkElement elem)
        {
            ElementInfo _info = new ElementInfo()
            {
                ElementType = elem.GetType(),
                ElementID = "NULL",
                Element = elem,
            };

            elem.Style = MojHost.GetHost.LoadStyle(_info);
        }
    }
}
